// PluginTreeView.cs 
// 
//  Copyright (C) 2009 FreeZzaph
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using Gtk;
using LibFreeZzaph;

namespace FreeZzaphGUI
{
	
	internal class PluginTreeView : TreeViewColumn
	{	
		public PluginTreeView()
			: base()
		{
			CellRendererPixbuf imageCellRenderer = new CellRendererPixbuf();
			PackStart(imageCellRenderer, false);
			SetCellDataFunc(imageCellRenderer, new TreeCellDataFunc(RenderPluginIcons));
			
			CellRendererText textCellRenderer = new CellRendererText();
			PackStart(textCellRenderer, true);
			SetCellDataFunc(textCellRenderer, new TreeCellDataFunc(RenderPluginTexts));
		}
		
		private void RenderPluginIcons(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			PluginTreeNode node = model.GetValue(iter, 0) as PluginTreeNode;
			CellRendererPixbuf imageCell = cell as CellRendererPixbuf;
			
			Style style = column.TreeView.Style;
			
			if (node.Category != null)
			{
				// It's a category node
				IconSet iconSet = style.LookupIconSet("gtk-file");
				imageCell.Pixbuf = iconSet.RenderIcon(style, TextDirection.Ltr, StateType.Normal, IconSize.Button, null, null);
			}
			else if (node.CategoryGroup != null)
			{
				// It's a category group node
				IconSet iconSet = style.LookupIconSet("gtk-directory");
				imageCell.Pixbuf = iconSet.RenderIcon(style, TextDirection.Ltr, StateType.Normal, IconSize.Button, null, null);
			}
			else if (node.Plugin != null)
			{
				// It's a plugin node
				// TODO: Find a way for the plug-in to supply an icon
				IconSet iconSet = style.LookupIconSet("gtk-execute");
				imageCell.Pixbuf = iconSet.RenderIcon(style, TextDirection.Ltr, StateType.Normal, IconSize.Button, null, null);
			}
		}
		
		private void RenderPluginTexts(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			PluginTreeNode node = model.GetValue(iter, 0) as PluginTreeNode;
			CellRendererText textCell = cell as CellRendererText;
			
			if (node.Category != null)
			{
				// It's a category node
				textCell.Text = node.Category.Description;
			}
			else if (node.CategoryGroup != null)
			{
				// It's a category group node
				textCell.Text = node.CategoryGroup.Description;
			}
			else if (node.Plugin != null)
			{
				// It's a plugin node
				textCell.Text = node.Plugin.Title;
			}
			else
			{
				// The tree is empty
				textCell.Text = "No plugins have been loaded";
				textCell.Style = Pango.Style.Italic;
			}
		}
	}
}
